<?php
/**
 * 自动编号（合同、回款、回访、发票）
 *
 * @author qifan
 * @date 2020-12-09
 */

namespace app\crm\traits;

use app\crm\model\NumberSequence;
use think\Db;

trait AutoNumberTrait
{
    private $stringToDate = ['yyyyMMdd' => 'Ymd', 'yyyy' => 'Y', 'yyyyMM' => 'Ym'];

    /**
     * 获取自动编号
     *
     * @param $type 1合同；2回款；3回访；4发票
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAutoNumbers($type)
    {
        $number = '';
        $data   = [];

        $info = Db::name('crm_number_sequence')->where('number_type', $type)->where('status', 0)->select();

        foreach ($info AS $key => $value) {
            # 文本
            if ($value['type'] == 1) {
                $number .= $value['value'] . '-';
            }
            # 日期
            if ($value['type'] == 2) {
                $number .= date($this->stringToDate[$value['value']]) . '-';
            }
            # 数字
            if ($value['type'] == 3) {
                $number .= $value['last_number'] . '-';

                # 需要更新的数据
                $data[] = [
                    'number_sequence_id' => $value['number_sequence_id'],
                    'last_number'        => $value['last_number'] + $value['increase_number'],
                    'last_date'          => time()
                ];

            }
        }

        return ['number' => rtrim($number, '-'), 'data' => $data];
    }
}